package servlet.log;

import common.Page;
import pojo.Department;
import pojo.Log;
import pojo.LogFamily;
import pojo.User;
import service.LogService;
import service.serviceImpl.LogServiceImpl;
import servlet.BaseServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-11-23.
 */
public class LogServlet extends BaseServlet{
    private static int PAGE_SIZE=10;
    LogService logService =new LogServiceImpl();
    int result=0;
    public  String writeLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //查询出日志的类别，返回到前端

        List<LogFamily> logFamilies= logService.getLogFamily();

        request.setAttribute("logFamilies",logFamilies);
        return "/jsp/log/writeLog.jsp";
    }
    public String submitSucc(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        Log log=new Log();
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("userInfo");
        log.setTitle(request.getParameter("title"));
        log.setContext(request.getParameter("context"));
        log.setLogFamily(request.getParameter("logFamily2"));
        log.setUserId(user.getId());
        log.setUsername(user.getUsername());
        log.setDate(new Date());
        log.setRealName(user.getRealName());
        log.setDepartment(user.getDepartment());
        int result= logService.writeLog(log);
        return "/log?method=selectCondition";
    }

    public String selectCondition(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("userInfo");
        List<LogFamily> logFamilies= logService.getLogFamily();
        session.setAttribute("logFamilies",logFamilies);
        String logFamily=request.getParameter("logFamily");
        session.setAttribute("logFamily",logFamily);
        String logStatus=request.getParameter("logStatus");
        request.setAttribute("logStatus",logStatus);

        //先获取总记录数
        Page page=new Page();
        if (logFamily==""&&logStatus==""||logFamily==null&&logStatus==null){

            List<Log> logs= logService.getAllLog(user.getId());
            int pageNow=page.currentPageNow(request, logs.size());
            List<Log> logs1= logService.getCurrentLog(user.getId(),pageNow,PAGE_SIZE);
            request.setAttribute("logs1",logs1);
            request.setAttribute("count",logs.size());

            return "/jsp/log/myLog.jsp";
        }
        if (logFamily!=""&&logStatus!=""){
            List<Log> logs= logService.getAllLogByLogFamilyAndLogStatus(user.getId(),logFamily,logStatus);
            int pageNow=page.currentPageNow(request, logs.size());
            List<Log> logs1= logService.getDoubleConditionLog(user.getId(),pageNow,PAGE_SIZE,logFamily,logStatus);
            request.setAttribute("logs1",logs1);
            request.setAttribute("count",logs.size());
            return "/jsp/log/myLog.jsp";
        }
        if (logFamily!=""&&logStatus==""){
            List<Log> logs= logService.getAllLogByLogFamily(user.getId(),logFamily);
            int pageNow=page.currentPageNow(request, logs.size());
            List<Log> logs1= logService.getFirstConditionLog(user.getId(),pageNow,PAGE_SIZE,logFamily);
            request.setAttribute("logs1",logs1);
            request.setAttribute("count",logs.size());
            return "/jsp/log/myLog.jsp";
        }
        if (logFamily==""&&logStatus!=""){
            List<Log> logs= logService.getAllLogByLogStatus(user.getId(),logStatus);
            int pageNow=page.currentPageNow(request, logs.size());
            List<Log> logs1= logService.getSecondConditionLog(user.getId(),pageNow,PAGE_SIZE,logStatus);
            request.setAttribute("logs1",logs1);
            request.setAttribute("count",logs.size());
            return "/jsp/log/myLog.jsp";
        }

        return null;
    }
    //查看日志
    public String lookLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        int id= Integer.parseInt(request.getParameter("id"));
        Log log= logService.lookCurrentLog(id);
        request.setAttribute("log",log);

        return "/jsp/log/manageDelLog.jsp";
    }
    //读取需要修改日志
    public String editLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        int id= Integer.parseInt(request.getParameter("id"));

        Log log= logService.lookCurrentLog(id);
        List<LogFamily> logFamilies= logService.getLogFamily();
        request.setAttribute("logFamilies",logFamilies);
        request.setAttribute("log",log);
        return "/jsp/log/editLog.jsp";
    }
    //修改日志
    public String againSubmit(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        Log log=new Log();

        String logStatus="正在上报";
        int id= Integer.parseInt(request.getParameter("id"));
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("userInfo");
        log.setTitle(request.getParameter("title"));
        log.setContext(request.getParameter("context"));
        log.setLogFamily(request.getParameter("logFamily1"));
        log.setUserId(user.getId());
        log.setUsername(user.getUsername());
        log.setDate(new Date());
        log.setId(id);
        //修改后  需要重新审核
        log.setLogStatus(logStatus);

        int result= logService.updateLog(log);
        //如果成功，则更新成功
        if(result>0){
            request.setAttribute("result",result);
            return "/log?method=selectCondition";
        }
        return "submitFaile";
    }
    //删除日志
    public String deleteLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //先获取id，后删除id日志
        int id= Integer.parseInt(request.getParameter("id"));
        int deleteResult= logService.deleteLog(id);

        if(deleteResult>0){
            request.setAttribute("deleteResult",deleteResult);
            return "/log?method=selectCondition";
        }
        return "submitFaile";
    }

    public String checkLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //将需要审核的日志查出来
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("userInfo");
        String logStatus="正在上报";
        String department=user.getDepartment();
        Page page=new Page();
        List<Log> logs= logService.checkAllLog(logStatus,department);
        int pageNow=page.currentPageNow(request,logs.size());
        request.setAttribute("count",logs.size());
        List<Log> logs1= logService.checkLog(logStatus,department,pageNow,PAGE_SIZE);
        request.setAttribute("logs1",logs1);
        return "/jsp/log/checkLog.jsp";
    }
    //查看审核的日志
    public String seeLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        int id=Integer.parseInt(request.getParameter("id"));
        Log log=logService.getLog(id);
        request.setAttribute("log",log);
        return "/jsp/log/seeLog.jsp";
    }
    public String check(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //审核是否通过
        int id=Integer.parseInt(request.getParameter("id"));
        String logStatus= request.getParameter("select");
        int result=logService.check(id,logStatus);
        request.setAttribute("result",result);

        return "/log?method=checkLog";
    }
    //查询本部门的日志
    public String lookPassLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        String logFamily=request.getParameter("logFamily");
        request.setAttribute("logFamily",logFamily);
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("userInfo");
        String department=user.getDepartment();
        Page page=new Page();
        String logStatus="审核通过";
        if (logFamily==""||logFamily==null){
            List<Log> logs= logService.checkAllLog(logStatus,department);
            int pageNow=page.currentPageNow(request, logs.size());
            List<Log> logs1= logService.getCurrentPassLog(logStatus,department,pageNow,PAGE_SIZE);
            request.setAttribute("logs1",logs1);
            request.setAttribute("count",logs.size());
            return "/jsp/log/lookPassLog.jsp";
        }
        if (logFamily!=""){
            List<Log> logs= logService.getPassLogByLogFamily(logFamily,department,logStatus);
            int pageNow=page.currentPageNow(request, logs.size());
            List<Log> logs1= logService.getFirstConditionCurrentPassLog(logStatus,department,logFamily,pageNow,PAGE_SIZE);
            request.setAttribute("logs1",logs1);
            request.setAttribute("count",logs.size());
            return "/jsp/log/lookPassLog.jsp";
        }
        return null;
    }
    //查询所有用户的日志
    public String manageLog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //查出所有的日志
        HttpSession session=request.getSession();
        //得到部门数
        List<Department> departments= logService.selectDepartment();
        request.setAttribute("departments",departments);
        //得到日志类别
        List<LogFamily> logFamilies=logService.selectLogFamily();
        request.setAttribute("logFamilies",logFamilies);
        String department=request.getParameter("department");
        request.setAttribute("department",department);
        String logFamily=request.getParameter("logFamily");
        request.setAttribute("logFamily",logFamily);
        //先获取总记录数
        Page page=new Page();
        //没有查询条件时
        if (department==""&&logFamily==""||department==null&&logFamily==null){
            List<Log> logList= logService.selectAllLog();
            int pageNow=page.currentPageNow(request, logList.size());
            List<Log> logs= logService.getCurrentAllLog(pageNow,PAGE_SIZE);
            request.setAttribute("logs",logs);
            request.setAttribute("count",logList.size());

            return "/jsp/log/manageAllLog.jsp";
        }
        //两个条件同时具备时查询
        if (department!=""&&logFamily!=""){
            List<Log> logList= logService.selectAllLogByDoubleCondition(department,logFamily);
            int pageNow=page.currentPageNow(request, logList.size());
            List<Log> logs= logService.getCurrentLogByDepartmentAndLogFamily(department,logFamily,pageNow,PAGE_SIZE);
            request.setAttribute("logs",logs);
            request.setAttribute("count",logList.size());

            return "/jsp/log/manageAllLog.jsp";
        }
        if (department!=""&&logFamily==""){
            List<Log> logList= logService.selectAllLogByFirstCondition(department);
            int pageNow=page.currentPageNow(request, logList.size());
            List<Log> logs= logService.getCurrentLogByDepartment(department,pageNow,PAGE_SIZE);
            request.setAttribute("logs",logs);
            request.setAttribute("count",logList.size());

            return "/jsp/log/manageAllLog.jsp";
        }
        if (department==""&&logFamily!=""){
            List<Log> logList= logService.selectAllLogBySecoundCondition(logFamily);
            int pageNow=page.currentPageNow(request, logList.size());
            List<Log> logs= logService.getCurrentLogByLogFamily(logFamily,pageNow,PAGE_SIZE);
            request.setAttribute("logs",logs);
            request.setAttribute("count",logList.size());

            return "/jsp/log/manageAllLog.jsp";
        }
        return null;
    }
    //查看用户日志，然后决定是否删除
    public String lookUserLog(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int id=Integer.parseInt(request.getParameter("id"));
        Log log=logService.lookCurrentLog(id);
        request.setAttribute("log",log);
        return "/jsp/log/manageDelLog.jsp";
    }
    public String manageDelLog(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int id=Integer.parseInt(request.getParameter("id"));
        result=logService.deleteLog(id);
        return "/log?method=manageLog";
    }

}
